package com.tronsis.distribution.wxapi;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tronsis.distribution.Constants;
import com.tronsis.distribution.R;
import com.tronsis.distribution.eventbus.EventMessage;
import com.tronsis.library.util.LogUtil;
import com.tronsis.library.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * 微信登录 分享 回调页面
 *
 * @author aiden@tronsis.com
 * @date 2018/4/10 9:29
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final int RETURN_TYPE_LOGIN = 1;
    private static final int RETURN_TYPE_SHARE = 2;

    public static final String STATE_BIND = "amoour_wx_bind";
    public static final String STATE_LOGIN = "amoour_wx_login";

    public static final String TAG = "WXEntryActivity";
    private IWXAPI mIWXApi;
    private Context mContext;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;

        mIWXApi = WXAPIFactory.createWXAPI(mContext, Constants.WX_APP_ID, true);
        mIWXApi.registerApp(Constants.WX_APP_ID);
        mIWXApi.handleIntent(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == RETURN_TYPE_LOGIN) {
            switch (baseResp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    SendAuth.Resp resp = ((SendAuth.Resp) baseResp);
                    String code = resp.code;
                    String state = resp.state;
                    LogUtil.e("WXEntryActivity", "code = " + code + ", state = " + resp.state);

                    if (!TextUtils.isEmpty(code)) {
                        /*if (STATE_LOGIN.equals(state)) {
                            startActivity(new Intent(mContext, LoginActivity.class).putExtra("code", code));
                        } else if (STATE_BIND.equals(state)) {
                            startActivity(new Intent(mContext, PersonalActivity.class).putExtra("code", code));
                        }*/
                    }
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    ToastUtil.showMessage(mContext, R.string.cancel_login);
                    break;
                default:
                    LogUtil.e("WXEntryActivity", "errCode = " + baseResp.errCode);
                    break;
            }
            finish();
        } else if (baseResp.getType() == RETURN_TYPE_SHARE) {
            switch (baseResp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    ToastUtil.showMessage(WXEntryActivity.this, getString(R.string.share_success));
                    EventBus.getDefault().post(new EventMessage(0x87, "share_success"));
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    ToastUtil.showMessage(WXEntryActivity.this, getString(R.string.share_cancel));
                    break;
                case BaseResp.ErrCode.ERR_AUTH_DENIED:
                    ToastUtil.showMessage(WXEntryActivity.this, getString(R.string.share_fail));
                    break;
                default:
                    ToastUtil.showMessage(WXEntryActivity.this, getString(R.string.share_fail));
                    break;
            }
            finish(); // 注意在：如果不finishing会有一层白色的activity
        }

    }
}
